package com.thh.dao;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author thh
 * @Description
 * @Version 1.0
 **/
@Repository
@Transactional(rollbackFor = Exception.class)
public class OrderDataBaseDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public void saveOrder(JSONObject orderInfo) throws  Exception{
        String sql = "insert into table_order (order_id,user_id,order_content) values(?,?,?)";
        // 添加订单记录
        int count = jdbcTemplate.update(sql,orderInfo.get("orderId"),
                orderInfo.get("userId"),orderInfo.get("orderContent"));
        if(count != 1){
            throw  new Exception("订单创建失败，原因【数据操作失败】。");
        }

        // 将信息存放到本地消息表，记录消息的状态
        saveLocalMessage(orderInfo);

    }

    /*
     * @Author 徐柯老师
     * @Tel/微信：15074816437
     * @Description 保存消息到本地消息表中
     * @Date 17:22 2020/4/29
     * @Param [orderInfo]
     * @return void
     **/
    public void saveLocalMessage(JSONObject orderInfo) throws  Exception{
        String sql = "insert into table_distribute_message (unique_id,msg_content,msg_status) values(?,?,?)";
        // 添加订单记录
        int count = jdbcTemplate.update(sql,orderInfo.get("orderId"),
                orderInfo.toString(),0);
        if(count != 1){
            throw  new Exception("出现异常，原因【数据操作失败】。");
        }
    }
}
